package cstest.entity;

import static org.junit.Assert.*;

import java.io.File;
import java.net.InetAddress;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import cs.entity.User;
import cs.entity.UserFile;
import cs.entity.UserFile.State;

public class TestUserFile {
	UserFile 	uf2,  // for constructor
				uf1 ; // for other tests
	User u1 ; 
	File f1 ; 
	@Before
	public void setUp() throws Exception {
		u1 = new User("username",InetAddress.getByName("127.0.0.1")) ;
		f1 = new File("test.txt"); 
		uf1 = new UserFile(u1, f1); 
		
	}

	@After
	public void tearDown() throws Exception {
		u1 = null ; 
		f1 = null ; 
		uf1 = null ; 
	}

	@Test
	public void testUserFile() {
		uf1 = new UserFile(u1, f1);
		assertTrue(uf1.getFile() == f1 ) ; 
		assertTrue(uf1.getState() == State.REQUEST) ; 
		assertTrue(uf1.getRemoteUser() == u1); 
		assertSame(uf1.getSize(), f1.length());
		assertSame(uf1.getName(), f1.getName());

	}

	@Test
	public void testGetRemoteUser() {
		assertTrue(uf1.getRemoteUser().equals(u1));
	}

	@Test
	public void testSetRemoteUser() {
		User u2 = new User(); 
		assertTrue(uf1.getRemoteUser().equals(u1));
		uf1.setRemoteUser(u2) ; 
		assertFalse(uf1.getRemoteUser().equals(u1));
		assertTrue(uf1.getRemoteUser().equals(u2));


	}

	@Test
	public void testGetFile() {
		assertTrue(uf1.getFile().equals(f1));
	}

	@Test
	public void testSetFile() {
		assertTrue(uf1.getFile().equals(f1));
		File f2 = new File("test2.txt");
		uf1.setFile(f2); 
		assertTrue(uf1.getFile().equals(f2));

	}

	@Test
	public void testGetProgression() {
		assertTrue(uf1.getProgression() == 0);
		uf1.setProgression(50); 
		assertTrue(uf1.getProgression() == 50);

	}

	@Test
	public void testSetProgression() {
		assertTrue(uf1.getProgression() == 0);
		uf1.setProgression(50); 
		assertTrue(uf1.getProgression() == 50);
	}

	@Test
	public void testSetState() {
		assertTrue(uf1.getState() == State.REQUEST) ; 
		uf1.setState(State.FAILED); 
		assertTrue(uf1.getState() == State.FAILED) ; 


	}

	@Test
	public void testGetState() {
		assertTrue(uf1.getState() == State.REQUEST) ; 
		uf1.setState(State.FAILED); 
		assertFalse(uf1.getState() == State.REQUEST) ; 

		assertTrue(uf1.getState() == State.FAILED) ; 

	}

	

	@Test
	public void testGetName() {
		assertTrue(uf1.getName().equals(f1.getName())) ; 
	}

	@Test
	public void testSetName() {
		assertTrue(uf1.getName().equals(f1.getName())) ; 
		assertFalse(uf1.getName().equals("test2.txt") ) ; 
		uf1.setName("test2.txt"); 
		assertFalse(uf1.getName().equals(f1.getName())) ; 
		assertTrue(uf1.getName().equals("test2.txt") ) ; 
	}
	@Test
	public void testGetSize() {
		assertTrue(uf1.getSize() == f1.length() ) ; 
	}

	@Test
	public void testSetSize() {
		assertTrue(uf1.getSize() == f1.length() ) ; 
		assertFalse(uf1.getSize() == 3000 ) ; 
		uf1.setSize(3000) ; 
		assertTrue(uf1.getSize() == 3000 ) ; 
		assertFalse(uf1.getSize() == f1.length() ) ; 

	}

}
